//
// Created by root on 4/29/17.
//

#include "runner_translate.h"

static void test_extract_when_type_is_sym() {
    Statement *s = init_sym("abc");
    assert_null(extract(s));
}

static void test_extract_when_type_is_loop() {
    Statement *s = init_loop();
    add_statement(s, "abc", LOOP);
    assert_null(extract(s));
}

static void test_extract_when_type_is_arg() {
    Statement *s = parse("{abc}");
    assert_string_equal(extract(s), "abc");
}

static void test_extract_when_type_is_arg_with_space() {
    Statement *s = parse("{abc def}");
    assert_string_equal(extract(s), "abc def");
}

static void test_arglist_with_sym() {
    Statement *s = parse("{there is {arg} surround sym {a}, followed by {b}}");
    assert_string_equal(arglist(s), "arg a b");
}

static void test_arglist_without_sym() {
    Statement *s = parse("{{a} {b} {c}}");
    assert_string_equal(arglist(s), "a b c");
}

//loop cannot placed here
static void test_arglist_when_has_loop() {
    Statement *s = parse("{{a} [{b}] {c}}");
    assert_string_equal(arglist(s), "a c");
}

static void test_target_with_sym() {
    Statement *s = parse("{there is {arg} surround sym {a}, followed by {b}}");
    assert_string_equal(target(s), "{arg} {a} {b}");
}

static void test_target_without_sym() {
    Statement *s = parse("{{a} {b} {c}}");
    assert_string_equal(target(s), "{a} {b} {c}");
}

//loop cannot placed here
static void test_target_when_has_loop() {
    Statement *s = parse("{{a} [{b}] {c}}");
    assert_string_equal(target(s), "{a} {c}");
}

static void test_funcname_none_null() {
    Statement *s = parse("{(sum {x} {y})}");
    assert_string_equal(funcname(s), "sum");
}

static void test_funcname_null() {
    Statement *s = parse("{sum {x} {y}}");
    assert_null(funcname(s));
}

int main(int argc, char **argv) {
    const struct CMUnitTest tests[] = {
            cmocka_unit_test(test_extract_when_type_is_sym),
            cmocka_unit_test(test_extract_when_type_is_loop),
            cmocka_unit_test(test_extract_when_type_is_arg),
            cmocka_unit_test(test_extract_when_type_is_arg_with_space),
            cmocka_unit_test(test_arglist_with_sym),
            cmocka_unit_test(test_arglist_without_sym),
            cmocka_unit_test(test_arglist_when_has_loop),
            cmocka_unit_test(test_target_with_sym),
            cmocka_unit_test(test_target_without_sym),
            cmocka_unit_test(test_target_when_has_loop),
            cmocka_unit_test(test_funcname_none_null),
            cmocka_unit_test(test_funcname_null),
    };
    return cmocka_run_group_tests(tests, setup_translate, teardown_translate);
}
